package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.hr.pojo.Dictionary;
import cn.hr.pojo.SalaryPro;
import cn.hr.pojo.SysUser;
import cn.hr.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.DictionaryService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典控制器
 * @author MO
 *
 */
@Controller
@RequestMapping("/sys/dictinary")
public class DictionaryController {
	private Logger logger = LoggerFactory.getLogger(DictionaryController.class);
	@Resource
	private DictionaryService dictionaryService;

	private String meagess;



	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("/showList")
	public String showList(@RequestParam(value = "pageIndex",required = false)String pageIndex,
					   @RequestParam(value = "type",required = false)String type,
					   @RequestParam(value = "valueName",required = false)String name,
					   Model model){
		Map<String,Object> map = new HashMap<>();
		int index = 1;
		if(pageIndex!=null&&pageIndex!="") {
			index = Integer.parseInt(pageIndex);
		}
		if (type!=null&&type!=""){
			map.put("type",type);
			model.addAttribute("type",type);
		}
/*		if (code!=null&&code!=""){
			map.put("code",code);
			model.addAttribute("code",code);
		}*/
		if (name!=null&&name!=""){
			map.put("valueName",name);
			model.addAttribute("valueName",name);
		}
		try {
			PageInfo<Map> pageInfo = dictionaryService.findListByCondition(map,index);
			model.addAttribute("pageInfo",pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "dictionary/showList";
	}

	@RequestMapping("/list")
	public String list(){
		return "dictionary/dictionaryList";
	}

	/**
	 * 修改
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateSave")
	@ResponseBody
	public String updateSave(Dictionary dictionary, HttpSession session){
		dictionary.setModifyById(((SysUser) (session.getAttribute(Constants.USER_SESSION))).getId());
		dictionary.setModifyDate(new Date());
		try {
			meagess = "false";
			if(dictionaryService.update(dictionary)){
				meagess = "true";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meagess;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam(value = "id")String id){
		try {
			if (dictionaryService.delete(Integer.parseInt(id))){
				meagess = "true";
			}else{
				meagess = "false";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  meagess;
	}

	/**
	 * 按类型查询
	 * @param type
	 * @return
	 */
	@RequestMapping("/listType")
	@ResponseBody
	public Object listType(@RequestParam("type")String type){
		List<Dictionary> list = null;
		try {
			list = dictionaryService.selectAllByTypeName(type);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSON.toJSON(list);
	}
}
